package com.kalvan.web;

import com.kalvan.web.servlet.interceptor.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import javax.annotation.Resource;

/**
 * @author chenliang
 */
@Slf4j
@Component
public class WebMvcConfig implements WebMvcConfigurer {

    String[] excludes = new String[]{"/plugins/**", "/images/**", "/js/**", "/css/**", "/error"};

    @Resource
    TokenInterceptor tokenInterceptor;
    @Resource
    PermissionInterceptor permissionInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("执行InterceptorRegistry");
        registry.addInterceptor(new CallInterfaceInterceptor()).addPathPatterns("/**").excludePathPatterns(excludes);
        registry.addInterceptor(new ParameterRequestInterceptor()).addPathPatterns("/**").excludePathPatterns(excludes);
        registry.addInterceptor(new CrossInterceptor()).addPathPatterns("/**");
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns(excludes);
        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**").excludePathPatterns(excludes);
        registry.addInterceptor(new DecryptInterceptor()).addPathPatterns("/**").excludePathPatterns(excludes);
        //国际化
        registry.addInterceptor(new LocaleChangeInterceptor()).addPathPatterns("/**").excludePathPatterns(excludes);
        registry.addInterceptor(new MessageResourceInterceptor()).addPathPatterns("/**").excludePathPatterns(excludes);
    }

    /**
     * springboot不拦截静态资源需配置
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Bean
    public LocaleResolver localeResolver() {
        CookieLocaleResolver slr = new CookieLocaleResolver();
        slr.setCookieMaxAge(3600);
        slr.setCookieName("Language");
        return slr;
    }

//    /**
//     * 跨域支持
//     *
//     * @return
//     */
//    private CorsConfiguration addCorsConfig() {
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//        if ("test".equals(active)) {
//            log.info("测试环境不限制ajax请求");
//            List<String> list = new ArrayList<>();
//            list.add("*");
//            corsConfiguration.setAllowedOrigins(list);
//        }
//        corsConfiguration.addAllowedOrigin(StringUtils.isBlank(domain) ? "*" : domain);
//        corsConfiguration.addAllowedHeader("*");
//        corsConfiguration.addAllowedMethod("*");
//        return corsConfiguration;
//    }
//
//    @Bean
//    public CorsFilter corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", addCorsConfig());
//        return new CorsFilter(source);
//    }
}
